import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes.js'
import store from '@/store'
import { Message } from "element-ui"

Vue.use(VueRouter)

const router = new VueRouter({
  base:process.env.BASE_URL,
  mode:"hash",
  routes
})

router.beforeEach((to,form,next)=>{
  if(to.meta.public){
    next();
    return;
  }
  if(store.state.token){
    next();
    return;
  }
  Message.error("未授权访问，请先登录")
  next("/login")
})

export default router
